//
//  CollectionController.m
//  网易新闻
//
//  Created by dllo on 15/10/13.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "CollectionController.h"
#import "Settings.h"
#import "DetailsController.h"
@interface CollectionController()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)NSMutableArray *dicArr;
@property(nonatomic, retain)UITableView *table;
@end

@implementation CollectionController

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.barTintColor = [[Settings shareSettings]color:WhiteToGray];
    self.view.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    self.table.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tabBarController.tabBar.hidden = YES;
    [self.table reloadData];
}

- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationItem.rightBarButtonItem = nil;
    self.title = @"";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dicArr = [[NSMutableArray alloc]initWithArray:[[Settings shareSettings]collectionsArr]];
    [self createTableView];
    self.title = @"我的收藏";
}

- (void)createTableView{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 65, 375, 600) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:self.table];
    [self.table release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"]autorelease];
    NSDictionary *dic = [self.dicArr objectAtIndex:indexPath.row];
    cell.textLabel.text = [dic objectForKey:@"title"];
    cell.textLabel.textColor = [[Settings shareSettings]color:BlackToWhite];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.detailTextLabel.text = [dic objectForKey:@"ptime"];
    cell.detailTextLabel.textColor = [UIColor lightGrayColor];
    cell.backgroundColor = [[Settings shareSettings]color:WhiteToLightGray];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dicArr.count;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    [self.table setEditing:editing animated:animated];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.dicArr removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    [[Settings shareSettings]changeCollectionArr:self.dicArr];
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSDictionary *dic = [[self.dicArr objectAtIndex:sourceIndexPath.row]retain];
    [self.dicArr removeObjectAtIndex:sourceIndexPath.row];
    [self.dicArr insertObject:dic atIndex:destinationIndexPath.row];
    [dic release];
    [[Settings shareSettings]changeCollectionArr:self.dicArr];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailsController *result = [[DetailsController alloc]init];
    result.docid = [self.dicArr[indexPath.row]objectForKey:@"docid"];;
    [self.navigationController pushViewController:result animated:YES];
    [result release];
}

- (void)dealloc{
    _table.delegate = nil;
    _table.dataSource = nil;
    [_dicArr release];
    [_table release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
